package com.atguigu.tingshu.user.startegy;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.user.startegy
 * @Project：tingshu_parent240829
 * @name：ProcessTrackOrderTypePayRecord
 * @Date：2025/2/13 16:31
 * @Filename：ProcessTrackOrderTypePayRecord
 */
@Component("1002")
@Slf4j
public class ProcessTrackOrderTypePayRecord implements ProcessDiffPayRecord{
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        // user_paid_track表中插入数据
        String orderNo = userPaidRecordVo.getOrderNo();

        UserPaidTrack userPaidTrack = userPaidTrackMapper.selectOne(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getOrderNo, orderNo));
        if (userPaidTrack != null) {
            throw new GuiguException(201, "该声音类型订单支付流水已存在");
        }
        Result<TrackInfo> trackInfoResult = albumInfoFeignClient.getTrackInfoById(userPaidRecordVo.getItemIdList().get(0));
        TrackInfo trackInfoData = trackInfoResult.getData();
        Long albumId = trackInfoData.getAlbumId();
        Assert.notNull(trackInfoData, "远程查询专辑微服务失败");
        //todo 声音有多个要进行循环
        userPaidRecordVo.getItemIdList().stream().forEach(trackId -> {
            UserPaidTrack userPaidTrack1 = new UserPaidTrack();
            userPaidTrack1.setOrderNo(orderNo);
            userPaidTrack1.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack1.setAlbumId(albumId);
            userPaidTrack1.setTrackId(trackId);
            userPaidTrackMapper.insert(userPaidTrack1);
        });
    }
}